<template>
  <div id="add">
    <!-- 头部 -->
    <div class="navebar">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="设备编号">
          <el-input
            v-model="formInline.taskCode"
            placeholder="请输入"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">
            查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格新建按钮 -->
    <div class="boxs">
      <div class="btn">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          class="btn1"
          @click="centerDialogVisible = true"
        >新建</el-button>
      </div>
      <!-- 表格 -->
      <div class="tabel bos">
        <el-table
          v-loading="loading"
          stripe
          tooltip-effect="dark"
          :data="SearchList"
          style="width: 100%"
          :header-cell-style="{
            //标题头部的颜色
            background: '#f5f7fa',
            height: '45px',
          }"
        >
          <el-table-column fixed type="index" label="序号" width="110" />
          <el-table-column prop="name" label="型号名称" width="150" />
          <el-table-column prop="model" label="型号编码" width="160" />
          <el-table-column label="设备图片" width="170">
            <template #default="{ row }">
              <img :src="row.image" alt="" class="imgphoto">
            </template>
          </el-table-column>
          <el-table-column prop="vmRow" label="货道行" width="170" />
          <el-table-column prop="vmCol" label="货道列" width="170" />
          <el-table-column
            prop="channelMaxCapacity"
            label="设备容量"
            width="170"
          />
          <el-table-column fixed="right" label="操作" width="160">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="medium "
                @click="cargo(scope.row)"
              >修改</el-button>
              <el-button
                type="text"
                size="medium "
                style="color: red"
                @click="delgo(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 实现分页
        current-page 当前页数
        page-size 每页显示个数
        total 总条目数
        current-change 页面改变时会触发
        prev-text="上一页"
        next-text="下一页"
         -->
        <div class="block">
          <el-pagination
            background
            :current-page.sync="pageOption.pageIndex"
            :page-size="pageOption.pagesize"
            layout="total,  pager, prev, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 新建弹出框 -->
    <div class="dils">
      <el-dialog
        title="title"
        :visible.sync="centerDialogVisible"
        width="53%"
        :before-close="closeFn"
      >
        <!-- 表单内容 -->
        <el-form
          ref="departRef"
          :model="departForm"
          :rules="departRules"
          label-width="120px"
        >
          <el-form-item label="型号名称" prop="name">
            <el-input
              v-model="departForm.name"
              style="width: 85%"
              placeholder="请输入"
              maxlength="15"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="型号编码" prop="model">
            <el-input
              v-model="departForm.model"
              style="width: 85%"
              placeholder="请输入"
              maxlength="15"
              show-word-limit
            />
          </el-form-item>
          <el-form-item label="货道行数" prop="vmRow">
            <el-input-number
              v-model="departForm.vmRow"
              style="width: 85%"
              controls-position="right"
              :min="1"
              :max="10"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item label="货道列数" prop="vmCol">
            <el-input-number
              v-model="departForm.vmCol"
              style="width: 85%"
              controls-position="right"
              :min="1"
              :max="10"
              @change="handleChange2"
            />
          </el-form-item>
          <el-form-item label="货道容量" prop="channelMaxCapacity">
            <el-input-number
              v-model="departForm.channelMaxCapacity"
              style="width: 85%"
              controls-position="right"
              :min="1"
              :max="10"
              @change="handleChange3"
            />
          </el-form-item>
          <el-form-item label="设备图片" prop="image">
            <!-- 图片上传组件 -->
            <UploadImg v-model="departForm.image" />
            <div style="font-size: 14px;color: #bac0cd; margin-top: -7px;">支持扩展名：jpg、png，文件不得大于100kb</div>
          </el-form-item>
        </el-form>
        <!-- 底部按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button class="btnquxiao" @click="closeFn">取 消</el-button>
          <el-button
            type="primary"
            class="btn1"
            @click="submitWork"
          >确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { searchDeviceType, addvmtype, alertvmtype } from '@/api'
import UploadImg from '@/components/UploadImg'
import { Message } from 'element-ui'
export default {
  components: {
    UploadImg
  },
  data() {
    return {
      loading: false,
      deltacticsVisible: false,
      centerDialogVisible: false,
      SearchList: [], // 表格
      currentPage1: 10,
      total: 0,
      typeId: null, // 存储售货机id
      // 搜索框
      formInline: {
        taskCode: ''
      },
      // 搜索传入的数值
      pageOption: {
        pageIndex: 1,
        pagesize: 10
      },
      // 售货机状态枚举
      vm: {
        0: '未投放',
        1: '运营',
        3: '撤机'
      },
      num: '',
      // 新增点位数据
      departForm: {
        name: '', // 型号名称
        model: '', // 型号编码
        vmRow: '', // 货道行数
        vmCol: '', // 货道列数
        channelMaxCapacity: '' // 货道容量
      },
      // 表单校验
      departRules: {
        name: [
          { required: true, message: '区域名称不能为空', trigger: ['blur'] }
        ],
        addr: [
          { required: true, message: '请填写详细地址', trigger: ['blur'] }
        ],
        areaCode: [
          { required: true, message: '请选择地址', trigger: ['blur'] }
        ],
        regionId: [
          { required: true, message: '请选择区域', trigger: ['blur'] }
        ],
        businessId: [
          { required: true, message: '请选择商圈', trigger: ['blur'] }
        ],
        ownerId: [
          { required: true, message: '请选择合作商', trigger: ['blur'] }
        ]
      }
    }
  },
  created() {
    this.searchDevice()
  },
  methods: {
    // 页面初始化请求数据
    async searchDevice() {
      const { data } = await searchDeviceType(this.pageOption)
      console.log(data)
      this.SearchList = data.currentPageRecords
      this.total = +data.totalCount
      this.loading = false
    },

    // 查询
    async onSubmit() {
      console.log('submit!')
      this.loading = true
      const { data } = await searchDeviceType({
        ...this.pageOption,
        name: this.formInline.taskCode
      })
      this.SearchList = data.currentPageRecords
      this.total = +data.totalCount
      this.loading = false
    },

    // 修改
    // 点击修改弹窗开启
    // 传入点击行的数据，赋值给表单绑定的变量
    // 将售货机id存储起来
    // 实现数据回显
    cargo(val) {
      this.centerDialogVisible = true
      console.log(val)
      this.departForm = val
      this.typeId = val.typeId
    },

    // 点击确定
    // 请求接口
    // 如果存在售货机id，调用修改售货机接口
    // 如果没有售货机id，调用新增售货机接口
    // 提示用户
    // 关闭弹窗
    // 刷新页面
    async submitWork() {
      if (!this.typeId) {
        await addvmtype(this.departForm)
        Message.success('新增成功')
      } else {
        await alertvmtype(this.departForm)
        Message.success('修改成功')
      }
      this.centerDialogVisible = false
      this.searchDevice()
    },

    // 删除
    delgo() {},

    // 关闭弹窗
    closeFn() {
      this.centerDialogVisible = false
      this.departForm = {
        name: '',
        model: '',
        vmRow: '',
        vmCol: '',
        channelMaxCapacity: ''
      }
    },

    handleChange(value) {
      console.log(value)
    },
    handleChange2(value) {
      console.log(value)
    },
    handleChange3(value) {
      console.log(value)
    },

    // 实现分页
    handleCurrentChange(val) {
      this.loading = true
      console.log(val)
      this.pageOption.pageIndex = val
      this.getDeviceList()
      this.loading = false
    }
  }
}
</script>

<style lang="scss">
#add {
  overflow: hidden;
  background-color: #f8fafd;
}
.navebar {
  margin: 20px 19px 0;
  padding-top: 14px;
  padding-left: 18px;
  height: 64px;
  background-color: #fff;
}
// 表格
.boxs {
  margin: 20px 19px 0;
  padding: 17px 0 1px 18px;
  background-color: #fff;
}
// 按钮
.btn1 {
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: #f8fafd;
  height: 35px;
}
.btn2 {
  background-color: #fbf4f0;
  border: #f8fafd;
  color: #f97138;
  height: 35px;
}
// 表格高度
.el-table .el-table__cell {
  padding: 3px 0 0 0;
}
// 图片
.imgphoto {
  display: block;
  width: 24px;
  height: 24px;
  -o-object-fit: cover;
  object-fit: cover;
  border-radius: 50%;
  background: #f3f6fb;
  border: 1px solid #f3f6fb;
}
// 内容
.el-descriptions {
  padding: 0px 0px 0px 30px;
}
.dils .el-dialog__footer {
  margin-top: -8px;
  padding: 10px 248px 20px;
}
.dils .el-dialog__body {
  padding: 30px 46px;
}
.dils .el-descriptions-item__label .has-colon {
  margin-right: 19px;
}
.dialog-footer {
  margin-top: 10px;
}
.btnquxiao {
  margin-right: 20px;
  height: 37px;
}
.button {
  background-color: #fbf4f0;
  border: #f8fafd;
  color: #f97138;
}
.dils .el-dialog__footer {
  margin-top: -8px;
  padding: 10px 248px 20px;
}
// 分页器
.block {
  margin: 20px 0;
}
.tabel {
  margin-top: 19px;
}
.btn-prev {
  margin-left: 100px;
}
// 图片上传
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
</style>
